package cn.smbms.controller;

import cn.smbms.pojo.User;
import cn.smbms.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Resource
    private UserService userService;

    @RequestMapping(value = "/login.html",method = RequestMethod.GET)
    public String toLogin(){
        return "login";
    }

    @RequestMapping(value = "/login.html",method = RequestMethod.POST)
    public String doLogin(String userCode, String userPassword, HttpSession session, HttpServletRequest request){

        User user = userService.login(userCode,userPassword);

        //登录失败
        if (null == user){
            request.setAttribute("error","登录失败！用户名或密码错误！");
            return "login";
        }

        session.setAttribute("userSession",user);

        return "redirect:main.html";
    }

    @RequestMapping(value = "/logout.html")
    public String logout(HttpSession session){

        //清空Session
        session.removeAttribute("userSession");

        return "redirect:login.html";
    }

    @RequestMapping(value = "/main.html")
    public String main(){
        return "frame";
    }

}
